#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import threading
import urllib.request
from util import utils
from bs4 import BeautifulSoup

url_1 = "https://www.t66y.com/thread0806.php?fid=16"
other_url = "https://www.t66y.com/thread0806.php?fid=16&search=&page="

root = "img/"





def getPatten(patt, html):
    rex = re.compile(patt)
    return re.findall(rex, html)


def getImg(html):
    reg = 'src="(.+?\.jpg)" alt='
    imgre = re.compile(reg)
    imglist = re.findall(imgre, html)
    x = 0
    for imgurl in imglist:
        urllib.urlretrieve(imgurl, '%s.jpg' % x)
        x += 1
    return imglist


def nameFitler(s):
    if(s.__contains__('>')):
        imgre = re.compile(r'(?<=\"\>).+\b')
        imglist = re.findall(imgre, s)
        return imglist[0]
    return s

def getImgFormPage(urlList, root):
    for url in urlList:
        pt = "https://www.t66y.com/"+ url
        print(pt)
        creatLocalfolder(pt, root, str(nameFitler(urlList[url])))


def getTextInA(content):
    if str(content).__contains__("</font>"):
        res = r'>(.*?)</font>'
    else:
        res = r'>(.*?)</a>'
    return re.findall(res, content, re.S | re.M)


def spiderpage(pagetext):
    L = {}
    t1 = BeautifulSoup(pagetext, 'html.parser').find_all('a')
    for t2 in t1:
        s = getTextInA(str(t2))[0]
        if str(t2.get('href')).__contains__('htm_data'):
            if (str(s).__contains__("::")):
                print()
            else:
                L[str(t2.get('href'))] = str(nameFitler(s))
    for i in L:
        print(i,L[i])
    return L


# 过滤出网页中url
def getUrlForImg(html):
    return spiderpage(html)


# 过滤出网页中的图片
def getAllImg(html):
    reg = r'data-src=\'(.+?\.jpg)\'| '
    imgre = re.compile(reg)
    imglist = imgre.findall(html)

    return imglist


# 保存图片
def saveImages(imglist, name):
    number = 1
    for imageURL in imglist:
        splitPath = imageURL.split('.')
        print(imageURL)
        fTail = splitPath.pop()
        if len(fTail) > 3:
            fTail = 'jpg'
        fileName = name + "/" + str(number) + "." + fTail
        # 对于每张图片地址，进行保存
        try:
            utils.downLoad(imageURL, fileName)
        except urllib.request.URLError as e:
            print(e.reason)
        number += 1


# 将图片保存在本地
def creatLocalfolder(url, root, folderName):
    path = str(root) + str(folderName)
    utils.mkdir(path)
    saveImages(getAllImg(utils.GBK(url)), path)


def run(i,isPage):
    print('thread %s.[%s]' % (threading.current_thread().name, str(i)))

    if(isPage == 0):
        path = root
    else:
        path = root + str(i)+'/'

    print(path)
    if i == 1:
        getImgFormPage(getUrlForImg(utils.GBK(url_1)), path)
    elif 197 >= i > 1:
        getImgFormPage(getUrlForImg(utils.GBK(other_url + str(i))), path)
